﻿using AdminStyem.Application.Command.CultivationCommand;
using AdminStyem.Domain.Entity;
using AdminStyem.Repositories;
using AutoMapper;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.CultivatioHandler
{
    public class CultivationAddCommandHandler : IRequestHandler<CultivationAddCommand, APIResult<object>>
    {
        IBaseRespository<Cultivation> BaseCultivation;
        IMapper mapper;

        public CultivationAddCommandHandler(IMapper mapper, IBaseRespository<Cultivation> BaseCultivation)
        {
            this.mapper = mapper;
            this.BaseCultivation = BaseCultivation;
        }

        public Task<APIResult<object>> Handle(CultivationAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> ret = new APIResult<object>();
            ret.Code = ResultCode.添加成功;
            ret.Message = ResultCode.添加成功.ToString();
            var list=mapper.Map<Cultivation>(request);
            int i = BaseCultivation.add(list).Result;
            if (i <= 0)
            {
                ret.Code = ResultCode.添加成功;
                ret.Message = ResultCode.添加成功.ToString();
                return Task.FromResult(ret);
            }
            return Task.FromResult(ret);
        }
    }
}
